package com.fish.system.dao;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.fish.common.dao.BaseIbatisDao;
import com.fish.system.po.ComLog;
@SuppressWarnings("unchecked")
public class ComLogDAO extends BaseIbatisDao {

	private static Map<String, String> parentTypeMap = new HashMap<String, String>();
	static {
		parentTypeMap.put("ORD_REFUNDMENT","ORD_ORDER");
		parentTypeMap.put("PROD_RELATION","PROD_PRODUCT");
		parentTypeMap.put("ORD_ORDER_ITEM_META","ORD_ORDER");
		parentTypeMap.put("ORD_ORDER","ORD_ORDER");
		parentTypeMap.put("ORD_INVOICE","ORD_ORDER");
		parentTypeMap.put("PROD_PRODUCT_ITEM","PROD_PRODUCT");
		parentTypeMap.put("META_B_CERTIFICATE","META_PRODUCT");
		parentTypeMap.put("ORDER_PERSON","ORD_ORDER");
		parentTypeMap.put("MARK_DIST_CHANNEL","MARK_DIST_CHANNEL");
		parentTypeMap.put("PASS_CODE","ORD_ORDER");
		parentTypeMap.put("PROD_TIME_PRICE","PROD_PRODUCT");
		parentTypeMap.put("META_TIME_PRICE","META_PRODUCT");
		parentTypeMap.put("META_SETTLEMENT","META_PRODUCT");
		parentTypeMap.put("META_PERFORM","META_PRODUCT");
		parentTypeMap.put("ORD_SETTLEMENT_QUEUE_ITEM","ORD_ORDER");
	}
	
	public Long queryListAllByCount(Map<String,Object> param) {
		return (Long)this.getSqlMapClientTemplate().queryForObject("COM_LOG.selectAllByCount", param);
	}
	
	public List<ComLog> queryListAll(Map<String,Object> param) {
		List<ComLog> list=this.getSqlMapClientTemplate().queryForList("COM_LOG.selectAll", param);
		return list;
	}
	
	public List<ComLog> queryByParentIdAndObjectType( String parentId, String parentType, String objectType) {
		ComLog par = new ComLog();
		par.setObjectType(objectType);
		par.setParentId(parentId);
		par.setParentType(parentType);
		List<ComLog> list = super.getSqlMapClientTemplate().queryForList("COM_LOG.selectByParentIdAndObjectType", par);
		return list;
	}
	
	public List<ComLog> queryByParentId(String objectType, String objectId) {
		ComLog par = new ComLog();
		par.setObjectType(objectType);
		par.setObjectId(objectId);
		List<ComLog> list = super.getSqlMapClientTemplate().queryForList("COM_LOG.selectByParentId", par);
		return list;
	}
	public List<ComLog> queryByObjectId(String objectType, String objectId) {
		ComLog par = new ComLog();
		par.setObjectType(objectType);
		par.setObjectId(objectId);
		List<ComLog> list = super.getSqlMapClientTemplate().queryForList("COM_LOG.select", par);
		return list;
	}
	
	public void insert(ComLog record) {
		//由于有太多的地方，插入ComLog时，未传入parentType，所以此处进行默认添加其parentType.
		if (record.getParentType()==null) {
			record.setParentType(parentTypeMap.get(record.getObjectType()));
		}
		String content = record.getContent();
		Long id = (Long) super.getSqlMapClientTemplate().insert("COM_LOG.insert", record);
	}
	
	
	/*********************************************通过制定参数查询结果集，并进行分页*************************************************/
	public List<ComLog> queryByParentIdAndObjectTypeMap(Map<String,Object> map){
		List<ComLog> list = super.getSqlMapClientTemplate().queryForList("COM_LOG.selectByParentIdAndObjectTypeMap", map);
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<ComLog> queryByParentIdMap(Map<String,Object> map){
		List<ComLog> list = super.getSqlMapClientTemplate().queryForList("COM_LOG.selectByParentIdMap", map);
		return list;
	}
	
	public List<ComLog> queryByObjectIdMap(Map<String,Object> map){
		List<ComLog> list = super.getSqlMapClientTemplate().queryForList("COM_LOG.selectMap", map);
		return list;
	}
	public Long queryByParentIdAndObjectTypeMapCount(Map<String,Object> map){
		return (Long)super.getSqlMapClientTemplate().queryForObject("COM_LOG.selectByParentIdAndObjectTypeMapCount", map);
	}
	public Long queryByParentIdMapCount(Map<String,Object> map){
		return (Long)super.getSqlMapClientTemplate().queryForObject("COM_LOG.selectByParentIdMapCount", map);
	}
	public Long queryByObjectIdMapCount(Map<String,Object> map){
		return (Long)super.getSqlMapClientTemplate().queryForObject("COM_LOG.selectMapCount", map);
	}

	/**
	 * 查询操作日志列表.
	 * 
	 * @param objectId
	 *            对象ID
	 * @param objectType
	 *            对象类型
	 * @param logType
	 *            日志类型
	 * @return 操作日志列表
	 */
	public List<ComLog> queryComLog(final Long objectId,
			final String objectType, final String logType) {
		final Map<String, Object> map = new Hashtable<String, Object>();
		map.put("objectId", objectId);
		map.put("objectType", objectType);
		map.put("logType", logType.toString());
		List<ComLog> list = getSqlMapClientTemplate().queryForList("COM_LOG.queryComLog",
				map);
		return list;
	}
	
	public List<String> queryObjectTypes(){
		return getSqlMapClientTemplate().queryForList("COM_LOG.queryObjectTypes");
	}

	public Long queryByMapCount(final Map<String,Object> map){
		return (Long)getSqlMapClientTemplate().queryForObject("COM_LOG.queryByMapCount",map);
	}

	

}